// index.ts
// 获取应用实例
const app = getApp<IAppOption>()
import { getDetailById, hasPermission } from '../../api/index'
Page({
  data: {
    type: '',
		UrlStatus: false,
		picDialog: false,
		audioStatus: 1, // 1 暂停， 2 播放,
		id: '',
  },
	onLoad(options) {
		const that = this
		if (!wx.getStorageSync('token')) {
			wx.redirectTo({ url: '/pages/login/login' })
			return
    }
		if (options && !options.id) {
			console.log('缺少参数')
			wx.redirectTo({ url: '/pages/index/index' })
			return
		}
		const id = options?.id
		this.setData({ id })

		if (options?.accessStatus == 1) {
			this.getDetail()
			return
		}
		wx.showLoading({ title: '',mask: true})
		hasPermission({
      documentId: id
    }).then(res => {
			wx.hideLoading()
			console.log(res)
      if (!res.data) {
				console.log('无权限')
				wx.redirectTo({ url: '/pages/index/index' })
				return
			}
			this.getDetail()
		}).catch(err => {
			wx.hideLoading()
			console.log('无权限1', err)
			wx.redirectTo({ url: '/pages/index/index' })
		})
	},
	onShow() {
		this.setData({
			UrlStatus: false,
			picDialog: false
		})
	},
	getDetail() {
		const that = this
		wx.showLoading({ title: '',mask: true})
		getDetailById({documentId: this.data.id}).then(res => {
			wx.hideLoading()
			wx.setNavigationBarTitle({
				title: res.data.title
			})
			this.setData({
				artData: res.data,
				type: res.data.type,
				path: res.data.path,
				fileType: res.data.fileType
			})
	
			if (res.data.fileType >= 9) {
				wx.nextTick(() => {
					that.audioCtx = wx.createAudioContext('myAudio')
				})
			}
			if (res.data.fileType >= 9) {
				wx.nextTick(() => {
					this.videoContext = wx.createVideoContext('myVideo')
				})
			}
			
		}).catch(err => {
			wx.hideLoading()
		})
	},
	play() {
		const res = this.data.artData
		if (res.type === 2) {
				this.setData({
					UrlStatus: true
				})
		} else {
			if (res.fileType >= 1 && res.fileType <= 7) {
				wx.downloadFile({
					url: `${res.path}`, //仅为示例，并非真实的资源
					success (res) {
						// 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
						console.log(res)
						if (res.statusCode == 200) {
							
							const filePath = res.tempFilePath
							wx.openDocument({
								filePath: filePath,
								success: function (res) {
									console.log('打开文档成功')
								}
							})
						}
					},
					fail(err) {
						console.log(err)
					}
				})
			}

			if (res.fileType == 8) {
				this.setData({
					picDialog: true
				})
			}
			if (res.fileType == 9) {
				this.setData({
					picDialog: true
				})
			}
			
		}
	},
	close(){
		this.setData({
			picDialog: false
		})
	},
	
	// {code: 1, text: 'DOC'},
  //   {code: 2, text: 'DOCX'},
  //   {code: 3, text: 'XLS'},
  //   {code: 4, text: 'XLSX'},
  //   {code: 5, text: 'PPT'},
  //   {code: 6, text: 'PPTX'},
  //   {code: 7, text: 'PDF'},
  //   {code: 8, text: 'IMAGE'},
  //   {code: 9, text: 'AUDIO'},
  //   {code: 10, text: 'VIDEO'},
  onShareAppMessage: function () {
    // 页面被用户分享时执行
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: 'EY汽车知识库，享你所想！'
        })
      }, 2000)
    })
    return {
      title: 'EY汽车知识库',
      path: '/page/index/index',
      promise 
    }
  },

	audioPlay: function () {
    this.audioCtx.play()
		this.setData({
			audioStatus: 2
		})
  },
  audioPause: function () {
    this.audioCtx.pause()
		this.setData({
			audioStatus: 1
		})
  },
  audioStart: function () {
    this.audioCtx.seek(0)
		this.audioPlay()
  },
	bindPlayVideo() {
    this.videoContext.play()
		this.playChange()
  },
	bindStopVideo() {
		this.videoContext.stop()
		this.stopChange()
	},
	bindseekVideo() {
		this.videoContext.seek(0);
		this.bindPlayVideo()
	},
	playChange() {
		this.setData({
			audioStatus: 2
		})
	},
	stopChange() {
		this.setData({
			audioStatus: 1
		})
	},

})
